package com.xu.demo.filterPattern;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FilterPattern {

    public static void main(String[] args) {

        //待过滤的数据
        List<Person> peoples = new ArrayList<>();
        peoples.add(new Person("Jenny", "F", 20));
        peoples.add(new Person("John", "M", 30));
        peoples.add(new Person("Jack", "M", 35));
        peoples.add(new Person("Mark", "M", 30));
        peoples.add(new Person("Ronnie", "M", 25));
        peoples.add(new Person("Ding", "M", 25));
        peoples.add(new Person("Janet", "F", 35));
        peoples.add(new Person("Jessie", "F", 25));

        //过滤器
        IFilter nameFilter = new NameFilter();
        IFilter ageFilter = new AgeFilter();
        IFilter sexFilter = new SexFilter();

        //过滤链
        FilterChain filter1 = new FilterChain(Arrays.asList(nameFilter, ageFilter));
        FilterChain filter2 = new FilterChain(Arrays.asList(ageFilter, sexFilter));
        FilterChain filter3 = new FilterChain(Arrays.asList(nameFilter, sexFilter));

        //过滤链过滤出数据
        List<Person> filteredPeople1 = filter1.doFilter(peoples);
        List<Person> filteredPeople2 = filter2.doFilter(peoples);
        List<Person> filteredPeople3 = filter3.doFilter(peoples);

        System.out.println("================名字以J开头和年龄大于25岁==================");
        filteredPeople1.forEach(System.out::println);
        System.out.println("================年龄大于25岁和性别是M男性==================");
        filteredPeople2.forEach(System.out::println);
        System.out.println("================名字以J开头和性别是M男性===================");
        filteredPeople3.forEach(System.out::println);
    }

}
